package com.lovecanon.jzjl.web.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lovecanon.jzjl.common.BaseService;
import com.lovecanon.jzjl.common.BtPage;
import com.lovecanon.jzjl.common.ResultData;
import com.lovecanon.jzjl.domain.TbUser;
import com.lovecanon.jzjl.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Map;

@Service
public class UserService  extends BaseService{

    @Autowired
    private UserRepository userRepository;


    public ResultData saveOrUpdate(TbUser tbUser){
        ResultData resultData = new ResultData();
        try {
            if (!StringUtils.isEmpty(tbUser.getId())){
                if (tbUser!=null){
                    userRepository.save(tbUser);
                }
            }else{
                userRepository.save(tbUser);
            }
            return  resultData.requestSuccess();
        }catch (Exception e){
            e.printStackTrace();
            return  resultData.requestError();
        }
    }

    public BtPage<List<TbUser>> findAll(Map<String,Object> map){
        int pageNum = (int)map.get("pageNumber");
        int pageSize =  (int)map.get("pageSize");
        BtPage<List<TbUser>> listBtPage = new BtPage<>();
        PageHelper.startPage(pageNum, pageSize);
        List<TbUser> all = userRepository.findAll();
        PageInfo<TbUser> pageInfo = new PageInfo<>(all);
        listBtPage.setCount((int) pageInfo.getTotal());
        listBtPage.setData(pageInfo.getList());
        return listBtPage;
    }

}
